package com.ruoyi.project.comp.h5.service;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 微信公众号SDK配置
 */
@Configuration
public class WxMpConfig {

    // 从配置文件读取微信公众号信息
    @Value("${wechat.appid}")
    private String appId;

    @Value("${wechat.appsecret}")
    private String appSecret;

    /**
     * 配置微信服务实例
     */
    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        WxMpConfigStorage configStorage = new WxMpDefaultConfigImpl();
        // 设置appId和appSecret
        ((WxMpDefaultConfigImpl) configStorage).setAppId(appId);
        ((WxMpDefaultConfigImpl) configStorage).setSecret(appSecret);
        wxMpService.setWxMpConfigStorage(configStorage);
        return wxMpService;
    }
}